package com.places.googleplacesandmaps;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ShowSingleMapActivity extends MapActivity {

	Place place;
	MapView mapView;
	List<Overlay> mapOverlays;
	AddItemizedOverlay itemizedOverlay;
	GeoPoint geoPoint;
	MapController mc;
	double lattitude;
	double longitude;
	
	OverlayItem overlayItem;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_single_map);
        
        Intent intent = getIntent();
        
        // Get the place's details
        place = (Place)intent.getSerializableExtra("place");
        
        // Users current geo location
        String user_latitude = intent.getStringExtra("user_latitude");
        String user_longitude = intent.getStringExtra("user_longitude");
                
        // Get Latitude and Longitude
        lattitude = place.geometry.location.lat;
        longitude = place.geometry.location.lng;
        
        // MapView and the Overlay
        mapView = (MapView)findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        mapOverlays=mapView.getOverlays();

		// These values are used to get map boundary area
		// The area where you can see all the markers on screen
		int minLat = Integer.MAX_VALUE;
		int minLong = Integer.MAX_VALUE;
		int maxLat = Integer.MIN_VALUE;
		int maxLong = Integer.MIN_VALUE;
		
        /****************************************
         *  Populating the user on the map
         ****************************************/
        // Geopoint to place the User's location on map
		geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),
				(int) (Double.parseDouble(user_longitude) * 1E6));
		
		// Drawable Red marker icon for the user
		Drawable drawable_user = this.getResources()
				.getDrawable(R.drawable.mark_red);
		
		itemizedOverlay = new AddItemizedOverlay(drawable_user, this);

		// Map overlay item
		overlayItem = new OverlayItem(geoPoint, "Your Location",
				"That is you!");

		itemizedOverlay.addOverlay(overlayItem);
		
		mapOverlays.add(itemizedOverlay);
		itemizedOverlay.populateNow();

        // calculating map boundary area
        minLat  = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
        minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
        maxLat  = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
        maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
		
		
        /****************************************
         *  Populating the Store on the map
         ****************************************/
        // Populate the Location Geo Points
        geoPoint = new GeoPoint((int)(lattitude*1E6), (int)(longitude*1E6));        
        
        // Drawable Marker Blue icon
        Drawable drawable=this.getResources().getDrawable(R.drawable.mark_blue);
        itemizedOverlay = new AddItemizedOverlay(drawable,this);

        // Overlay Items






        overlayItem = new OverlayItem(geoPoint, place.name, place.vicinity);
        itemizedOverlay.addOverlay(overlayItem);

        mapOverlays.add(itemizedOverlay);
        itemizedOverlay.populateNow();
                
        mc = mapView.getController();
        
        // calculating map boundary area
        minLat  = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
        minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
        maxLat  = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
        maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );

		
		// Adjusting the zoom level so that you can see all the markers on map
		mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
		
		// Showing the center of the map
		mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
		mapView.postInvalidate();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_show_single_map, menu);
        return true;
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
